package com.showde.shoppingmall.activity;

import com.kim.androidbase.tools.StringUtils;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.RegisterLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.utils.ToastUtils;
import com.showde.shoppingmall.view.ProgressDialog;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends BaseActivity {

	private static final String TAG = "RegisterActivity";

	private static final int MSG_REGISTER = 0x002;

	private EditText userAccountEditText;
	private EditText userPasswordEditText;
	private TextView register;

	private CheckBox checkBox;

	private String userAccount;
	private String password;

	ProgressDialog dialog;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_REGISTER:
				dismissDialog();
				break;
			}

			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.txt_register:
			register();
			break;

		default:
			break;
		}
	}

	@Override
	protected void initParam() {

	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_register);
		userAccountEditText = (EditText) findViewById(R.id.edt_useraccount);
		userPasswordEditText = (EditText) findViewById(R.id.edt_password);
		checkBox = (CheckBox) findViewById(R.id.ck_readprovision);
		register = (TextView) findViewById(R.id.txt_register);
		register.setOnClickListener(this);
	}

	private boolean prepare() {
		userAccount = userAccountEditText.getText().toString();
		password = userPasswordEditText.getText().toString();

		if (StringUtils.isEmptyOrNull(userAccount)
				|| StringUtils.isEmptyOrNull(password)) {
			ToastUtils.showToast(this, R.string.msg_account_not_complete);
			return false;
		}
		return true;
	}

	private void register() {
		if (!prepare()) {
			return;
		}

		if (!checkBox.isChecked()) {
			ToastUtils.showToast(this, R.string.msg_not_agree_provision);
			return;
		}

		if (dialog != null) {
			dialog.dismiss();
		}
		dialog = new ProgressDialog(this, "正在注册...", R.layout.view_progress);
		dialog.show();

		RegisterLogic.register(userAccount, password,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						handler.sendEmptyMessage(MSG_REGISTER);
					}

					@Override
					public void onError(String errorMsg) {
						handler.sendEmptyMessage(MSG_REGISTER);
					}
				});
	}

	private void dismissDialog() {
		if (dialog != null) {
			dialog.dismiss();
		}
	}

	@Override
	public void onBackPressed() {
		this.finish();
	}

}
